/*
 * @Overview:   loading
 * @Author:     Zi_Jun
 * @Email:      zijun2030@163.com
 * @Date:       2020/12/9 10:18
 * @Mark:       //
 */
import { IElLoadingOptions } from '/@/types/element';
import { deepMerge } from '/@/utils/common';
import { ElLoading } from 'element-plus';

let loading: any;

// 全视域加载配置
const elGlobalLoadingOptions = (): IElLoadingOptions => {
  const defaultOpt = {
    target: 'document.body',
    fullscreen: true,
    text: '加载中...',
  };
  return defaultOpt;
};

// 局部加载配置
const elAreaLoadingOptions = (options: IElLoadingOptions = {}): IElLoadingOptions => {
  const defaultOpt = {
    target: 'document.body',
    fullscreen: false,
    text: '',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 1)',
  };
  return deepMerge(defaultOpt, options);
};

// 开启全域loading
export const openLoading = () => {
  loading = ElLoading.service(elGlobalLoadingOptions());
  return loading;
};

// 关闭loading
export const closeLoading = () => {
  // setTimeout(() => {
  //   loading.close();
  // }, 3000);
  loading && loading.close();
};
